package com.tanhua.dubbo;

import com.tanhua.domain.mongo.Person;
import org.bson.types.ObjectId;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonTest {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Test
    public void testAdd(){
        Person person = new Person(new ObjectId(),"紫衫龙王金花婆婆",20,"波斯");
        Person person1 = new Person(new ObjectId(),"金毛狮王谢逊",30,"冰火岛");
        Person person2 = new Person(new ObjectId(),"青翼蝠王韦一笑",40,"明教万蝠堂");
        Person person3 = new Person(new ObjectId(),"白眉鹰王殷天正",50,"明教百鹰堂");
        Person person4=new Person(new ObjectId(),"武学绝顶张三丰",109,"武当山");
        mongoTemplate.save(person);
        mongoTemplate.save(person1);
        mongoTemplate.save(person2);
        mongoTemplate.save(person3);
        mongoTemplate.save(person4);

    }

    @Test
    public void testupdate(){
//        把username为zhangsan的年龄改成25
//        Query var1, Update var2, Class<?> var3
        Query query = new Query(Criteria.where("username").is("zhangsan"));
        Update update = new Update();
        update.set("age",25);
        mongoTemplate.updateFirst(query,update,Person.class);

        Query query1=new Query(Criteria.where("username").is("武学绝顶张三丰"));
        Update update1=new Update();
        update1.set("age",999);
        mongoTemplate.updateFirst(query1,update1,Person.class);

    }

    @Test
    public void testFind(){
//        Query query = new Query();
//        List<Person> personList = mongoTemplate.find(query, Person.class);
//        System.out.println(personList);
//
//        Person person = mongoTemplate.findById(new ObjectId("60dbe214e42db834d0d96564"), Person.class);
//        System.out.println(person);

//        每页显示2条数据
        Query query = new Query();
//        query.limit(2).skip(0); //第一页  page-1*pagesize
//        query.limit(2).skip(2); //第二页
        query.limit(2).skip(4).with(Sort.by(Sort.Direction.DESC,"age")); //第三页
        List<Person> personList1 = mongoTemplate.find(query, Person.class);
        System.out.println(personList1);

        Query query1=new Query();
        query1.limit(4).skip(3).with(Sort.by(Sort.Direction.DESC,"age"));
        List<Person>personList2=mongoTemplate.find(query1,Person.class);
        System.out.println(personList2);

    }


//    @Test
//    public void testdelete(){
//        mongoTemplate.remove(new Query(),Person.class);
//    }

    @Test
    public void testdelete(){
        mongoTemplate.remove(new Query(),Person.class);
    }


}
